package com.sum.common.panling

import android.util.Log
import com.sum.common.panling.PowerUtils.Companion.power
import com.sum.common.panling.PowerUtils.Companion.uart_switch
import com.sum.common.panling.PowerUtils.Companion.vol33
import com.sum.common.panling.PowerUtils.Companion.vol5
import com.sum.common.panling.PowerUtils.Companion.writeUhfFile
import java.io.FileOutputStream

class PowerUtils private constructor() {

    companion object {
        const val TAG = "PowerUtils"
        private const val vol33 = "/sys/devices/platform/module_power/rfid_en_3v3"
        private const val vol5 = "/sys/devices/platform/module_power/rfid_print_5v"
        private const val power = "/sys/devices/platform/module_power/vdd_io_host_en"
        private const val uart_switch = "/sys/devices/platform/module_power/uart_switch_1v8"

        // 使用 by lazy 创建单例
        val instance: PowerUtils by lazy { PowerUtils() }

        fun writeUhfFile(path: String?, value: ByteArray?) {
            var fos: FileOutputStream? = null
            try {
                fos = FileOutputStream(path)
                fos.write(value)
                fos.flush()
                fos.close()
            } catch (e: Exception) {
                e.printStackTrace()
                Log.d(TAG, "-----writeUhfFile-----e1=" + e.message)
            }
        }
    }

    fun panling_power_up() {
        Log.d(TAG, "panling_power_up:")
        try {
            writeUhfFile(uart_switch, "0".toByteArray())
            Thread.sleep(20)
            writeUhfFile(power, "1".toByteArray())
            Thread.sleep(20)
            writeUhfFile(vol33, "1".toByteArray())
            Thread.sleep(20)
            writeUhfFile(vol5, "1".toByteArray())
            Thread.sleep(10)
        } catch (ex: Exception) {
        }
    }

    fun panling_power_down() {
        Log.d(TAG, "panling_power_down:")
        try {
            writeUhfFile(vol5, "0".toByteArray())
            Thread.sleep(10)
            writeUhfFile(vol33, "0".toByteArray())
            Thread.sleep(10)
            writeUhfFile(power, "0".toByteArray())
            Thread.sleep(10)
            writeUhfFile(uart_switch, "1".toByteArray())
        } catch (ex: Exception) {
        }
    }
}

